package com.cardbaobao.cardbabyclient.widget.refresh;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshKernel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;

/**
 * Created by Jiangzx on 2017/7/25 10:40.
 */

public class FooterLoadMore extends LinearLayout implements RefreshFooter {
    public static final int STATE_PRE_LOADING=0;
    private String mStatePreLoadingStr="扯一下，还是有的";
    public static final int STATE_RELEASE_TO_LOAD=1;
    private String mStateReleaseToLoad="松开后加载更多";
    public static final int STATE_LOADING=2;
    private String mStateLoading ="卖力加载中，请稍候";
    private static final int STATE_LOAD_COMPLETE=3;
    private String mStateLoadComplete="加载完成";
    public static final int STATE_NO_MORE_DATA =4;
    private String mStateNoMoreData ="别扯了，到底了";

    private int mIcon=R.mipmap.icon_sweat;

    private int mState=0;

    private ImageView mImageView;
    private TextView mTextView;
    public FooterLoadMore(@NonNull Context context) {
        this(context,null);
    }

    public FooterLoadMore(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context,attrs,0);
    }

    public FooterLoadMore(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.widget_footer_load_more,this,true);
        mImageView= (ImageView)findViewById(R.id.iv);
        mTextView= (TextView) findViewById(R.id.tv);
    }

    @Override
    public void onPullingUp(float percent, int offset, int footerHeight, int extendHeight) {

    }

    @Override
    public void onPullReleasing(float percent, int offset, int footerHeight, int extendHeight) {

    }

    @Override
    public boolean setLoadmoreFinished(boolean finished) {
        return false;
    }

    @NonNull
    @Override
    public View getView() {
        return this;
    }

    @Override
    public SpinnerStyle getSpinnerStyle() {
        return SpinnerStyle.Translate;
    }

    @Override
    public void setPrimaryColors(int... colors) {

    }

    @Override
    public void onInitialized(RefreshKernel kernel, int height, int extendHeight) {

    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {

    }

    @Override
    public void onStartAnimator(RefreshLayout layout, int height, int extendHeight) {

    }

    @Override
    public int onFinish(RefreshLayout layout, boolean success) {
        return getResources().getInteger(R.integer.refreshDelay);
    }

    @Override
    public boolean isSupportHorizontalDrag() {
        return false;
    }

    @Override
    public void onStateChanged(RefreshLayout refreshLayout, RefreshState oldState, RefreshState newState) {
        switch (newState){
            case None:
            case PullToUpLoad:
                if(mState!=STATE_NO_MORE_DATA){
                    mState=STATE_PRE_LOADING;
                }
                break;
            case ReleaseToLoad:
                if(mState!=STATE_NO_MORE_DATA){
                    mState=STATE_RELEASE_TO_LOAD;
                }
                break;
            case Loading:
                if(mState!=STATE_NO_MORE_DATA){
                    mState=STATE_LOADING;
                }
                break;
            case LoadFinish:
                if(mState!=STATE_NO_MORE_DATA){
                    mState=STATE_LOAD_COMPLETE;
                }
                break;
        }
        configImageAndText();
    }

    /**
     * 设置没有更多数据时显示的文字
     * @param noMoreDataStr 无更多数据时显示的文本
     */
    public void setStateNoMoreDataStr(@NonNull String noMoreDataStr){
        this.mStateNoMoreData=noMoreDataStr;
    }

    /**
     * 设置上拉加载更多初始状态时的文字
     * @param preLoading
     */
    public void setStatePreLoadingStr(@NonNull String preLoading){
        this.mStatePreLoadingStr=preLoading;
    }

    /**
     * 隐藏提示文字前面的图标
     */
    public void hideImage(){
        this.mImageView.setVisibility(GONE);
    }

    /**
     * 显示提示文字前面的图标
     */
    public void showImage(){
        this.mImageView.setVisibility(VISIBLE);
    }

    private void configImageAndText(){
        switch (mState){
            case STATE_PRE_LOADING:
                mImageView.setImageResource(R.mipmap.icon_pull_gray);
                mTextView.setText(mStatePreLoadingStr);
                break;
            case STATE_RELEASE_TO_LOAD:
                mImageView.setImageResource(R.mipmap.icon_pull_gray);
                mTextView.setText(mStateReleaseToLoad);
                break;
            case STATE_LOADING:
                mImageView.setImageResource(R.drawable.anim_loading_view);
                ((AnimationDrawable)mImageView.getDrawable()).start();
                mTextView.setText(mStateLoading);
                break;
            case STATE_LOAD_COMPLETE:
                mImageView.setImageResource(R.drawable.anim_loading_view);
                ((AnimationDrawable)mImageView.getDrawable()).stop();
                mTextView.setText(mStateLoadComplete);
                break;
            case STATE_NO_MORE_DATA:
                mImageView.setImageResource(mIcon);
                mTextView.setText(mStateNoMoreData);
                break;
        }
    }

    /**
     *设置footer为没有更多数据状态
     */
    public void setStateNoMoreData(){
        this.mState= STATE_NO_MORE_DATA;
        configImageAndText();
    }

    /**设置footer为加载前的状态*/
    public void setStatePreLoading(){
        this.mState=STATE_PRE_LOADING;
        configImageAndText();
    }

    /**设置没有更多数据时footer显示的图标*/
    public void setNoMoreDataIcon(int mipmapResId){
        this.mImageView.setVisibility(VISIBLE);
        this.mIcon=mipmapResId;
        this.mImageView.setImageResource(mIcon);
    }
}
